import { Inject } from '@midwayjs/core';
import { CoolController, BaseController } from '@cool-midway/core';
import { VideoVideoEntity } from '../../entity/video';
import { VideoVideoService } from '../../service/video';
import { VideoCategoryEntity } from '../../entity/category';
import { BaseSysUserEntity } from '../../../base/entity/sys/user';

/**
 * 视频信息
 */
@CoolController({
  api: ['add', 'delete', 'update', 'info', 'list', 'page'],
  entity: VideoVideoEntity,
  service: VideoVideoService,
  pageQueryOp: {
    keyWordLikeFields: ['a.name', 'a.introduction'],
    fieldEq: ['a.status', 'a.format', 'a.categoryId'],
    where: async ctx => {
      const { minScore, maxScore, minHot, maxHot } = ctx.request.body;
      const where = [];
      if (minScore !== undefined) {
        where.push(['a.score >= :minScore', { minScore }]);
      }
      if (maxScore !== undefined) {
        where.push(['a.score <= :maxScore', { maxScore }]);
      }
      if (minHot !== undefined) {
        where.push(['a.hot >= :minHot', { minHot }]);
      }
      if (maxHot !== undefined) {
        where.push(['a.hot <= :maxHot', { maxHot }]);
      }
      return where;
    },
    join: [
      {
        entity: VideoCategoryEntity,
        alias: 'b',
        condition: 'a.categoryId = b.id',
        type: 'leftJoin',
      },
      {
        entity: BaseSysUserEntity,
        alias: 'c',
        condition: 'a.authorId = c.id',
        type: 'leftJoin',
      },
    ],
    select: ['a.*', 'b.name as categoryName', 'c.nickName as authorName'],
  },
})
export class AdminVideoVideoController extends BaseController {
  @Inject()
  videoVideoService: VideoVideoService;
}
